/*
 * @Author: askMeWhy
 * @Date:   2017-12-29 14:44:52
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-02-02 14:08:28
 */
import Vue from "vue";
import Vuex from "vuex";
import { merge } from "../utils/tool.kit";
import { localStorage } from "../utils/storage";

import * as getters from "./getters";
import * as mutations from "./mutations";
import * as actions from "./actions";
Vue.use(Vuex);
const storeGetters = () => {
    let obj = {};
    Object.keys(getters).forEach(key => {
        obj = merge(true, obj, getters[key]);
    });
    return obj;
};
const storeMutations = () => {
    let obj = {};
    Object.keys(mutations).forEach(key => {
        obj = merge(true, obj, mutations[key]);
    });
    return obj;
};
const storeActions = () => {
    let obj = {};
    Object.keys(actions).forEach(key => {
        obj = merge(true, obj, actions[key]);
    });
    return obj;
};

export default new Vuex.Store({
    strict: true,
    state: {
        ajaxLoader: false,
        ajaxLoaderIndex: 0,
        ajaxLoaderText: "",
        selectedControl: {
            key: "",
            addRouterKey: true
        },
        user: {
            userInfo: (() => {
                const userInfo: any = localStorage.getItem("userInfo");
                return userInfo || null;
            })(),
            auth: (() => {
                const auth: any = localStorage.getItem("auth");
                return auth || null;
            })(),
            isSignIn: (() => {
                const auth: any = localStorage.getItem("auth");
                return !!(auth && auth.token && auth.refreshToken);
            })()
        },
        dynamicRouterList: [],
        dynamicRouterRequest: false,
        authArea: (() => {
            const authArea: any = localStorage.getItem("authArea");
            return authArea || null;
        })(),
        authHistory: (() => {
            const authHistory: any = localStorage.getItem("authHistory");
            return authHistory || null;
        })(),
        aMap: null,
        menuList: [],
        collectList: [],
        allPagePath: {}
    },
    getters: storeGetters(),
    mutations: storeMutations(),
    actions: storeActions(),
    modules: {}
});
